﻿using System.Collections.Generic;
using System;

namespace Framework.AssetsManager
{
    public class AssetDataManager {


        private static AssetDataManager m_instance;

        public static AssetDataManager Instance
        {
            get
            {
                if (m_instance == null)
                    m_instance = new AssetDataManager();

                return m_instance;
            }
        }

        private Dictionary<string, AssetData> m_assetsDataDict = new Dictionary<string, AssetData>();

        private Dictionary<EResWhenLoad, Dictionary<string, AssetData>> m_filterDict = new Dictionary<EResWhenLoad, Dictionary<string, AssetData>>();


        private AssetDataManager()
        {
            Array res = Enum.GetValues(typeof(EResWhenLoad));

            for (int i = 0; i < res.Length; ++i)
            {
                m_filterDict.Add((EResWhenLoad)res.GetValue(i), new Dictionary<string, AssetData>());
            }
        }

        public void AddAssetData(string _name, AssetData _assetData)
        {
            if (m_assetsDataDict.ContainsKey(_name))
                m_assetsDataDict.Remove(_name);
            m_assetsDataDict.Add(_name, _assetData);

            Dictionary<string, AssetData> filterDict = m_filterDict[_assetData.whenLoad];
            if (filterDict.ContainsKey(_name))
                filterDict.Remove(_name);
            filterDict.Add(_name, _assetData);
        }

        public AssetData GetAssetData(string _name)
        {
            AssetData data = null;
            m_assetsDataDict.TryGetValue(_name, out data);

            return data;
        }

        public Dictionary<string, AssetData> GetAllDatas()
        {
            return m_assetsDataDict;
        }

        public Dictionary<string,AssetData>.ValueCollection.Enumerator GetDatasByWhenLoad(EResWhenLoad _whenLoad)
        {
            return m_filterDict[_whenLoad].Values.GetEnumerator();
        }

        public void Clear()
        {
            m_assetsDataDict.Clear();

            Dictionary<EResWhenLoad, Dictionary<string, AssetData>>.ValueCollection.Enumerator v = m_filterDict.Values.GetEnumerator();

            while (v.MoveNext())
            {
                v.Current.Clear();
            }
        }

        public void Remove(string _name)
        {
            EResWhenLoad type;
            if (m_assetsDataDict.ContainsKey(_name))
            {
                type = m_assetsDataDict[_name].whenLoad;
                m_assetsDataDict.Remove(_name);

                if (m_filterDict[type].ContainsKey(_name))
                    m_filterDict[type].Remove(_name);
            }

        }
    }
}